// 续班api
import http from './http';

// 个人续班报表
export const personalSearch = param => http.post('/api/renewal/personal/search', {
    ...param
});

// 大 小组续班报表
export const groupSearch = param => http.post('/api/renewal/group/search', {
    ...param
});

// 大小组下拉筛选
export const fetchSearchFormOptions = param => http.post('/api/agility/view/select', {
    ...param
});

const downloadByPost = (url, params, excelName = '', callback) => {
    http({
        url,
        method: 'post',
        data: {
            ...params,
        },
        responseType: 'blob',
    }).then(res => {
        const blob = new Blob([res], {
            type: 'application/vnd.ms-excel;',
        });
        const downloadElement = document.createElement('a');
        const href = window.URL.createObjectURL(blob);
        downloadElement.href = href;
        const time = new Date().getTime();
        downloadElement.download = `${excelName}${time}.xlsx`;
        downloadElement.click();
    })
        .finally(() => {
            callback && callback();
        });
};

// 大小组下载
export const fetchDownload = (param, callback) =>
    downloadByPost('/api/renewal/group/download', param, '', callback);

// 个人表格下载
export const fetchPersonalDownload = (param, callback) =>
    downloadByPost('/api/renewal/personal/download', param, '', callback);
